package com.xingkong.web.controller.product;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.core.page.TableDataInfo;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.product.domain.ProductPriceSpecifications;
import com.xingkong.product.service.IProductPriceSpecificationsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 商品规格价格关联Controller
 *
 * @author yiyue
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/product/price/specifications")
public class ProductPriceSpecificationsController extends BaseController {
    private String prefix = "product/price/specifications";

    @Autowired
    private IProductPriceSpecificationsService productPriceSpecificationsService;

    @RequiresPermissions("product:price:specifications:view")
    @GetMapping()
    public String specifications() {
        return prefix + "/specifications";
    }

    /**
     * 查询商品规格价格关联列表
     */
    @RequiresPermissions("product:price:specifications:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductPriceSpecifications productPriceSpecifications) {
        startPage();
        return getDataTable(productPriceSpecificationsService.list(new QueryWrapper<ProductPriceSpecifications>(productPriceSpecifications)));
    }

    /**
     * 导出商品规格价格关联列表
     */
    @RequiresPermissions("product:price:specifications:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductPriceSpecifications productPriceSpecifications) {
        List<ProductPriceSpecifications> list = productPriceSpecificationsService.list(new QueryWrapper<>());
        ExcelUtil<ProductPriceSpecifications> util = new ExcelUtil<ProductPriceSpecifications>(ProductPriceSpecifications.class);
        return util.exportExcel(list, "specifications");
    }

    /**
     * 新增商品规格价格关联
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品规格价格关联
     */
    @RequiresPermissions("product:price:specifications:add")
    @Log(title = "商品规格价格关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductPriceSpecifications productPriceSpecifications) {
        return toAjax(productPriceSpecificationsService.save(productPriceSpecifications));
    }

    /**
     * 修改商品规格价格关联
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductPriceSpecifications productPriceSpecifications = productPriceSpecificationsService.getById(id);
        mmap.put("productPriceSpecifications", productPriceSpecifications);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品规格价格关联
     */
    @RequiresPermissions("product:price:specifications:edit")
    @Log(title = "商品规格价格关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductPriceSpecifications productPriceSpecifications) {
        return toAjax(productPriceSpecificationsService.updateById(productPriceSpecifications));
    }

    /**
     * 删除商品规格价格关联
     */
    @RequiresPermissions("product:price:specifications:remove")
    @Log(title = "商品规格价格关联", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productPriceSpecificationsService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
